package com.fengchao.api.service;

import com.fengchao.api.dao.bci.BciArtistInfoMapper;
import com.fengchao.api.dao.mri.MriAlbumMapper;
import com.fengchao.api.dao.mri.MriMvMapper;
import com.fengchao.api.dao.mri.MriSongsMapper;
import com.fengchao.model.bci.BciArtistInfo;
import com.fengchao.model.mri.MriAlbum;
import com.fengchao.model.mri.MriMv;
import com.fengchao.model.mri.MriSongs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by wangnian on 2016/6/12.
 */
@Service
public class MriService {
    @Autowired
    MriSongsMapper mriSongsMapper;
    @Autowired
    MriMvMapper mriMvMapper;
    @Autowired
    MriAlbumMapper mriAlbumMapper;
    @Autowired
    BciArtistInfoMapper bciArtistInfoMapper;


    /**
     * 查询艺人的歌曲
     * @param artistId
     * @param page
     * @param count
     * @return
     */
    public List<Map<String, Object>> findSongsList(Integer artistId, Integer page, Integer count) {
        Integer start = (page - 1) * count;
        List<Map<String, Object>> list = new ArrayList<>();
        //获取歌曲的信息
        List<MriSongs> mriSongsList = mriSongsMapper.findMriSongsByArtistId(artistId, start, count);
        for (MriSongs mriSongs : mriSongsList) {

            Map<String, Object> map = new HashMap<>();
            map.put("songId", mriSongs.getId());
            map.put("songLink", mriSongs.getSongLink());
            map.put("songName", mriSongs.getSongName());
            map.put("songTime", mriSongs.getSongTime());
            map.put("songImageUrl", mriSongs.getSongImageUrl());
            map.put("songType", mriSongs.getSongType());
            map.put("songInfo", mriSongs.getSongInfo());
            //获取歌曲对应的艺人
            List<Map<String, String>> lists = bciArtistInfoMapper.findArtistBySongId(mriSongs.getId());
            //获取歌曲对应的专辑
            List<Map<String, String>> lists1 = mriAlbumMapper.findMriAlbumBySongId(mriSongs.getId());
            map.put("artistId", lists.size() > 0 ? lists.get(0).get("artistId") : "");
            map.put("nameDisplay", lists.size() > 0 ? lists.get(0).get("nameDisplay") : "");
            map.put("albumName", lists1.size() > 0 ? lists1.get(0).get("albumName") : "");
            map.put("albumId", lists1.size() > 0 ? lists1.get(0).get("albumId") : "");
            list.add(map);
        }
        return list;
    }


    public Integer findSongsListTotal(Integer artistId) {
        return mriSongsMapper.findMriSongsByArtistIdTotal(artistId);
    }


    /**
     * 获取所有的mv信息
     *
     * @param artistId
     * @param page
     * @param count
     * @return
     */
    public List<MriMv> findMvList(Integer artistId, Integer page, Integer count) {
        Integer start = (page - 1) * count;
        return mriMvMapper.findMriMvByArtistId(artistId, start, count);
    }


    public Integer findMvListTotal(Integer artistId) {
        return mriMvMapper.findMriMvByArtistIdTotal(artistId);
    }

    /**
     * 查询所有专辑
     *
     * @param artistId
     * @param page
     * @param count
     * @return
     */
    public List<Map<String, Object>> findAlbumList(Integer artistId, Integer page, Integer count) {
        Integer start = (page - 1) * count;
        List<Map<String, Object>> resultlist = new ArrayList<>();
        List<MriAlbum> list = mriAlbumMapper.findMriAlbumByArtistId(artistId, start, count);
        for (MriAlbum mriAlbum : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", mriAlbum.getId());
            map.put("albumName", mriAlbum.getAlbumName());
            map.put("albumImageUrl", mriAlbum.getAlbumImageUrl());
            map.put("albumInfo", mriAlbum.getAlbumInfo());
            map.put("publishCompany", mriAlbum.getPublishCompany());
            map.put("language", mriAlbum.getLanguage());
            map.put("publishDate", mriAlbum.getPublishDate());
            map.put("songType", mriAlbum.getSongType());
            resultlist.add(map);
        }
        return resultlist;
    }

    /**
     * 获取所有的专辑条数
     *
     * @param artistId
     * @return
     */
    public Integer findAlbumListTotal(Integer artistId) {
        return mriAlbumMapper.findMriAlbumByArtistIdTotal(artistId);
    }


    public List<Map<String, Object>> findSongsByAlbumIdList(Integer albumId, Integer page, Integer count) {
        Integer start = (page - 1) * count;
        List<Map<String, Object>> list = new ArrayList<>();
        //获取歌曲的信息
        List<MriSongs> mriSongsList = mriSongsMapper.findMriSongsByAlbumId(albumId, start, count);
        for (MriSongs mriSongs : mriSongsList) {
            Map<String, Object> map = new HashMap<>();
            map.put("songId", mriSongs.getId());
            map.put("songLink", mriSongs.getSongLink());
            map.put("songName", mriSongs.getSongName());
            map.put("songTime", mriSongs.getSongTime());
            map.put("songImageUrl", mriSongs.getSongImageUrl());
            map.put("songType", mriSongs.getSongType());
            map.put("songInfo", mriSongs.getSongInfo());
            //获取歌曲对应的艺人
            List<Map<String, String>> lists = bciArtistInfoMapper.findArtistBySongId(mriSongs.getId());
            //获取歌曲对应的专辑
            List<Map<String, String>> lists1 = mriAlbumMapper.findMriAlbumBySongId(mriSongs.getId());
            //map.put("songArtist", lists);
            map.put("songAlbum", lists1);
            map.put("artistId", lists.size() > 0 ? lists.get(0).get("artistId") : "");
            map.put("nameDisplay", lists.size() > 0 ? lists.get(0).get("nameDisplay") : "");
            map.put("albumName", lists1.size() > 0 ? lists1.get(0).get("albumName") : "");
            map.put("albumId", lists1.size() > 0 ? lists1.get(0).get("albumId") : "");
            list.add(map);
        }
        return list;
    }


    public Integer findSongsByAlbumIdListTotal(Integer albumId) {
        return mriSongsMapper.findMriSongsByAlbumIdTotal(albumId);
    }


    public Map<String, Object> findMriAlbumById(Integer albumId) {
        MriAlbum mriAlbum = mriAlbumMapper.selectByPrimaryKey(albumId);
        Map<String, Object> map = new HashMap<>();
        if (mriAlbum != null) {
            map.put("albumName", mriAlbum.getAlbumName());
            map.put("albumImageUrl", mriAlbum.getAlbumImageUrl());
            map.put("albumInfo", mriAlbum.getAlbumInfo());
            map.put("publishCompany", mriAlbum.getPublishCompany());
            map.put("language", mriAlbum.getLanguage());
            map.put("publishDate", mriAlbum.getPublishDate());
            map.put("songType", mriAlbum.getSongType());
            //获取艺人名字
            BciArtistInfo bciArtistInfo = bciArtistInfoMapper.selectByPrimaryKey(mriAlbum.getArtistId());
            map.put("artistDisplay", bciArtistInfo != null ? bciArtistInfo.getNameDisplay() : "");
            map.put("artistId", bciArtistInfo != null ? bciArtistInfo.getId() : "");
        }
        return map;
    }
}
